<template>
  <teleport to="body">
    <transition name="slide">
      <div class="add-song" v-show="visible">
        <div class="header">
          <h1 class="title">添加歌曲列表</h1>
          <div class="close" @click.stop="hide">
            <i class="icon-close"></i>
          </div>
        </div>
        <div class="search-input-wrapper">
          <SearchInput v-model="query" placeholder="搜索歌曲"></SearchInput>
        <div v-if="!query">
          <Switches :items="['最近播放', '搜索历史']" v-model="currentIndex"></Switches>
          <div class="list-wrapper">
            <Scroll v-if="currentIndex === 0" class="list-scroll" ref="scrollRef">
              <div class="list-inner">
                <SongList :songs="playHistory" @select="selectSongBySongList"> </SongList>
              </div>
            </Scroll>
            <Scroll v-if="currentIndex === 1" class="list-scroll" ref="scrollRef">
              <div class="list-inner">
                <searchList :searches="searchHistory" :showDelete="false" @select="addQuery"></searchList>
              </div>
            </Scroll>
          </div>
        </div>
        </div>
        <div class="search-result" v-show="query">
          <SuggestList :query="query" :showSinger="false" @select-song="selectSongBySuggest"></SuggestList>
        </div>
        <Message ref="messageRef">
            <div class="message-title">
                <i class="icon-ok"></i>
                <span class="text">该歌曲已添加至播放列表</span>
            </div>
        </Message>
      </div>
    </transition>
  </teleport>
</template>
<script>
import SearchInput from '@/components/search/search-input.vue'
import SuggestList from '@/components/search/suggest.vue'
import SearchList from '@/components/search/search-list.vue'
import SongList from '@/components/base//song-list/song-list.vue'
import Scroll from '@/components/base/scroll/scroll.vue'
import Switches from '@/components/base/switch/switch.vue'
import Message from '@/components/base/message/message.vue'
import useSearchHistory from '@/components/search/use-search-history'
import { ref, computed, nextTick } from 'vue'
import { useStore } from 'vuex'
export default {
  name: 'AddSong',
  components: {
    SearchInput,
    SearchList,
    SuggestList,
    SongList,
    Switches,
    Scroll,
    Message
  },
  setup () {
    const store = useStore()
    const scrollRef = ref(null)
    const messageRef = ref(null)
    const visible = ref(false)
    const query = ref('')
    const currentIndex = ref(0)

    const searchHistory = computed(() => store.state.searchHistory)
    const playHistory = computed(() => {
      return store.state.playHistory
    })

    function show () {
      visible.value = true
      nextTick(() => {
        scrollRef.value.scroll.scroll._value.refresh()
      })
    }
    function hide () {
      visible.value = false
    }
    function addQuery (s) {
      query.value = s
    }
    function selectSongBySongList ({ song }) {
      store.dispatch('addSong', song)
      messageRef.value.show()
    }
    function selectSongBySuggest (song) {
      store.dispatch('addSong', song)
      saveSearch(query.value)
      messageRef.value.show()
    }

    // hooks
    const { saveSearch } = useSearchHistory()

    return {
      scrollRef,
      messageRef,
      visible,
      query,
      currentIndex,
      searchHistory,
      playHistory,
      show,
      hide,
      addQuery,
      selectSongBySongList,
      selectSongBySuggest
    }
  }
}
</script>
<style lang="scss" scoped>
.add-song {
    position: fixed;
    top: 0;
    bottom: 0;
    width: 100%;
    z-index: 300;
    background: $color-background;
    .header {
      position: relative;
      height: 1.876333rem;
      text-align: center;
      .title {
        line-height: 1.876333rem;
        font-size: $font-size-large;
        color: $color-text;
      }
      .close {
        position: absolute;
        top: 0;
        right: .341151rem;
        .icon-close {
          display: block;
          padding: .511727rem;
          font-size: .852878rem;
          color: $color-theme;
        }
      }
    }
    .search-input-wrapper {
      margin: .852878rem
    }
    .list-wrapper {
      position: absolute;
      top: 7.036247rem;
      bottom: 0;
      width: 100%;
      left: 0;
      .list-scroll {
        height: 100%;
        overflow: hidden;
        .list-inner {
          padding: .852878rem 1.279318rem;
        }
      }
    }
    .search-result {
      position: fixed;
      top: 5.287846rem;
      bottom: 0;
      width: 100%;
    }
  }

  .message-title {
    text-align: center;
    padding: .767591rem 0;
    font-size: 0;
    .icon-ok {
      font-size: $font-size-medium;
      color: $color-theme;
      margin-right: .170576rem;
    }
    .text {
      font-size: $font-size-medium;
      color: $color-text;
    }
  }
</style>
